import java.text.*; import java.util.Date;
class StatisticsController {
    static int number = 235786
    def index = {}
    def getnumber = {
        number = number+1
        [number:number]
    }
    def init = {
        
        Calendar   test   =   Calendar.getInstance(); 
        def  year = [2010]
        for(int m=0;m<year.size();m++){
            
            for(int i=1;i<7;i++){
                test.set(year[m],i-1,1); 
                int   totalDay   =   test.getActualMaximum(Calendar.DAY_OF_MONTH);
                
                for(int y=1;y<totalDay+1;y++){
                    Statistics statistics = new Statistics()
                    statistics.time = year[m].toString()+i.toString()
                    statistics.day = y.toString()
                    String str = "123456789"; 
                    String str1 = "456"
                    Random random = new Random(); 
                    StringBuffer sb = new StringBuffer(); 
                    StringBuffer sb1 = new StringBuffer(); 

                    for (int x = 0; x < 3; ++x){ 
                        int number = random.nextInt(9);
                        sb.append(str.charAt(number)); 
                    }

                    for (int x = 0; x < 1; ++x){ 
                        int number = random.nextInt(3);
                        sb1.append(str1.charAt(number)); 
                    }

                    statistics.hit = (sb1.toString()+sb.toString()).toInteger()
                    statistics.save(flush:true)
                }
            }
            
        }
    }

    def stat = {
        def time = params.time
        if(!time){
            Date date = new Date()
            time =  ((date.getYear()+1900).toString()+(date.getMonth()+1).toString())
        }else{
            time = params.time
        }
        def statGroup = Statistics.list().groupBy{
            it.time
        }
        def statsList = Statistics.findAllByTime(time)
        [statsList:statsList,statGroup:statGroup,monthName:(time.substring(0,4)+"年"+time.substring(4)+"月")]
    }

    def initEveryDay = {
        Date date = new Date()
        for(int i=1;i<date.getDate()+1;i++){
            def time = ((date.getYear()+1900).toString()+(date.getMonth()+1).toString())
            def stat = Statistics.findByDayAndTime(i.toString(),time)
            if(stat){
                println ("YES"+stat.time+"aaaa"+stat.day)
            }else{
                Statistics statistics = new Statistics()
                statistics.time = time
                statistics.day = i

                String str = "123456789"; 
                String str1 = "456"
                Random random = new Random(); 
                StringBuffer sb = new StringBuffer(); 
                StringBuffer sb1 = new StringBuffer(); 

                for (int x = 0; x < 3; ++x){ 
                    int number = random.nextInt(9);
                    sb.append(str.charAt(number)); 
                }

                for (int x = 0; x < 1; ++x){ 
                    int number = random.nextInt(3);
                    sb1.append(str1.charAt(number)); 
                }

                statistics.hit = (sb1.toString()+sb.toString()).toInteger()
                statistics.save(flush:true)
            }
        }
        redirect(action:"stat")

    
    }
}
